package com.youdelu.road;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.youdelu.road.adapter.JTDListAdapter;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.utils.BroadcastManager;
import com.youdelu.road.utils.Util;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPicker;

public class JTDActivity extends AppCompatActivity implements  View.OnClickListener,JTDListAdapter.ClickCallBack{


    private TextView close ;
    private ListView list;
    private JTDListAdapter adapter;
    private Context mContext;
    private Gson gson ;
    private List<Attr> data = new ArrayList<>();
    public static final String SELECT_POSITION = "select_position"; //刷新地图
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jtd);
        mContext = this ;
        gson = new Gson();
        init();
    }
    public void init(){
        close = findViewById(R.id.close);
        close.setOnClickListener(this);
        list = findViewById(R.id.list);
        try {
            data = MainActivity.db.findAll(Selector.from(Attr.class).where("state",">","0"));
            //Log.e("data",gson.toJson(data));
        }catch (Exception e){
        }
        adapter = new JTDListAdapter(mContext,this,data);
        list.setAdapter(adapter);
    }

    @Override
    public void select(int position) {
        Attr attr = adapter.getAttr(position);
        try {
            MainActivity.db.update(attr);
        }catch (Exception e){
            //Log.e("update",e.getMessage());
        }
        BroadcastManager.getInstance(mContext).sendBroadcast(MainActivity.UPDATE_ATTR,attr);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.close:finish();break;
        }
    }

}
